<?php
/**
* @author trungdt
* @since sep 10, 2011
* 
* Change logs
* Sep 12, 2011 - trungdt - Add function: show data collected from other sermons modules
*/

class articlemodule_config {
	function form($object) {
        global $db;
        
		if (!defined('SYS_FORMS')) require_once(BASE.'subsystems/forms.php');
		exponent_forms_initialize();
		
		$form = new form();
		if (!isset($object->id)) {
			$object->enable_categories = 0;
			$object->allow_submissions = 0;
			$object->require_login = 0;
			$object->item_per_page = 10;
			// Sep 12, 2011 - trungdt - aggregate - show on multi module instance
            $object->aggregate = array();
			$object->force_no_aggregate = 1;
			$object->seo_name = unserialize($object->location_data)->src;
		} else {
			$form->meta('id',$object->id);
		}
	
		$form->register(null,'', new htmlcontrol('<h2>User Article Submission</h2>'));	
		$form->register('allow_submissions','Allow users to submit articles?',new checkboxcontrol($object->allow_submissions,true));
		$form->register('require_login','Require user to create account before submitting an article?',new checkboxcontrol($object->require_login,true));
		$form->register('item_per_page','How many item per page?',new textcontrol($object->item_per_page));
		$form->register(null,'', new htmlcontrol('<h2>Categories</h2>'));	
		$form->register('enable_categories','Enable Categories?',new checkboxcontrol($object->enable_categories,true));
		$form->register('seo_name','Seo name',new textcontrol($object->seo_name,60,false,200,'',1));
		// Sep 12, 2011 - begin
		$form->register(null,'', new htmlcontrol('<h2>Aggregate Articles</h2>'));    
		$loc = unserialize($object->location_data);
		$articles = exponent_modules_getModuleInstancesByType('articlemodule');
		$saved_aggregates = empty($object->aggregate) ? array() : unserialize($object->aggregate);
		$all_articles = array();
		$selected_articles = array();
		foreach ($articles as $src => $cal) {
            // now, we force not use module if force_no_aggregate
            $temp=null;
            $temp->mod="articlemodule";
            $temp->src=$src;
            $temp->int='';
            $temp=serialize($temp);
            $temp_config=$db->selectObject("articlemodule_config","location_data = '{$temp}'");
            if (!$temp_config)
                $temp_config->force_no_aggregate=1;
            
            if ($temp_config->force_no_aggregate == 1)
                continue;
            
            //
			$articles_name = (empty($cal[0]->title) ? $config->seo_name : $cal[0]->title);
            if ($articles_name == "Untitled")
                $articles_name=$temp_config->seo_name;
                
            $articles_name.=' on page '.$cal[0]->section;
			if ($src != $loc->src) {
				if (in_array($src, $saved_aggregates)) {
					$selected_articles[$src] = $articles_name;
				} else {
					$all_articles[$src] =  $articles_name;
				}
			}
		}
		$form->register('aggregate',"Merge articles from these other Articles Module",new listbuildercontrol($selected_articles,$all_articles));
        $form->register('force_no_aggregate','Force no aggregate',new checkboxcontrol($object->force_no_aggregate,true));
		// Sep 12, 2011 - end
		
		$form->register('submit','',new buttongroupcontrol('Save','','Cancel'));
		return $form;
	}
	
	function update($values,$object) {
        $object->enable_categories = empty($values['enable_categories']) ? 0 : 1;
		$object->force_no_aggregate = empty($values['force_no_aggregate']) ? 0 : 1;
		$object->allow_submissions = empty($values['allow_submissions']) ? 0 : 1;
		$object->require_login = empty($values['require_login']) ? 0 : 1;
		$object->item_per_page = $values['item_per_page'];
		// Change this later to do some better recalculation detection (more efficient)
		$object->recalc = 1;
		// Sep 12, 2011 - trungdt - Article Aggregate Begin
		$object->aggregate = serialize(listbuildercontrol::parseData($values,'aggregate'));
		$object->seo_name=_to_vietnamese_with_nomark(_ab_safe_html_string(trim($values['seo_name'])));
		// Sep 12, 2011 - trungdt - end
		return $object;
	}
}

?>
